﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using CoachingOrganizer.Logic.Models.Tools.EmotionsPack;

namespace CoachingOrganizer.Android.Adapters.Tools.EmotionsPack
{
    public class EmotionsPackAdapter : BaseExpandableListAdapter
    {
        private readonly Activity _context;
        private readonly EmotionsPackModel _model;
        private Dictionary<string, string[]> _emotionNames;

        public EmotionsPackAdapter(Activity context, EmotionsPackModel model)
        {
            _context = context;
            _model = model;
            _emotionNames = GetEmotionNames();
        }

        public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
        {
            return childPosition;
        }

        public override long GetChildId(int groupPosition, int childPosition)
        {
            return groupPosition * 1000 + childPosition;
        }

        public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
        {
            View view = _context.LayoutInflater.Inflate(Resource.Layout.EmotionLayout, parent, false);
            TextView emotionNameTextView = view.FindViewById<TextView>(Resource.Id.emotionNameTextView);
            emotionNameTextView.Text = _emotionNames.Values.ElementAt(groupPosition)[childPosition];
            view.Click += (sender, e) => _model.SetSelectedEmotionByName(emotionNameTextView.Text);
            return view;
        }

        public override int GetChildrenCount(int groupPosition)
        {
            return _emotionNames.ElementAt(groupPosition).Value.Length;
        }

        public override Java.Lang.Object GetGroup(int groupPosition)
        {
            return groupPosition;
        }

        public override long GetGroupId(int groupPosition)
        {
            return groupPosition;
        }

        public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
        {
            View view = _context.LayoutInflater.Inflate(Resource.Layout.EmotionGroupLayout, parent, false);
            TextView emotionGroupNameTextView = view.FindViewById<TextView>(Resource.Id.emotionGroupNameTextView);
            emotionGroupNameTextView.Text = _emotionNames.Keys.ElementAt(groupPosition);
            return view;
        }

        public override int GroupCount
        {
            get { return _emotionNames.Count; }
        }

        public override bool HasStableIds
        {
            get { return false; }
        }

        public override bool IsChildSelectable(int groupPosition, int childPosition)
        {
            return true;
        }

        private Dictionary<string, string[]> GetEmotionNames()
        {
            int[] ids = new int[] { Resource.Array.FearGroupEmotions, Resource.Array.AngerGroupEmotions, Resource.Array.GladnessGroupEmotions, Resource.Array.ShameGroupEmotions, Resource.Array.AnguishGroupEmotions }; //_context.Resources.GetIntArray(Resource.Array.Emotions);
            string[] groupNames = _context.Resources.GetStringArray(Resource.Array.EmotionsGroups);
            return ids.Select((id, i) => new { Id = id, Index = i }).ToDictionary(v => groupNames[v.Index], v => _context.Resources.GetStringArray(v.Id));
        }
    }
}